'use strict';
//获取数据库对象
const db = uniCloud.database()
exports.main = async (event, context) => {
	//event为客户端上传的参数
	// console.log('event : ', event);
	// const{user_name} = event;
	// console.log('user_name : ', user_name);
	//获取user表
	const collection = db.collection("user");
	
	let status = 0
	let msg = '';
	//查询到指定user的所有相关信息存放userInfo变量里
	const userInfo = await collection.where({
		user_name:event.user_name
	}).get();
	
// console.log("userInfo:",userInfo)
//userInfo: { data: [ { _id: 'cd045e75614dbb7e10cd1a4418347c09',password: '123456',user_name: 'trygo' } ], requestId: '17c184661e7_1' }

// console.log("userInfo.data:",userInfo.data)
//userInfo.data: [ { _id: 'cd045e75614dbb7e10cd1a4418347c09', password: '123456', user_name: 'trygo' } ]

// console.log("userInfo.data[0]:",userInfo.data[0])
//userInfo.data[0]: { _id: 'cd045e75614dbb7e10cd1a4418347c09',password: '123456',user_name: 'trygo' }


	 const user = userInfo.data;//data: [ { _id: 'cd045e75614dbb7e10cd1a4418347c09',password: '123456',user_name: 'trygo' } ]
	// console.log("user[0]:",user[0].password)
	
	
	if(user.length == 0){
		//用户名不存在
		status = 1;
		msg = "用户名不存在";
	}else if(user[0].password != event.password){
		//密码错误
		status = 2;
		msg = "密码错误";
	}else {
		msg='登录成功'
	}
	
	//返回数据给客户端
	return {
		"status":status,
		"userInfo":userInfo,
		"msg":msg
		};
};
